Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Azure DevOps VersionController #476

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

rerikson
Copy link

What does this change

This adds a new VersionController implementation for Azure DevOps

For example if it introduces a new flag or modifies a commands output, give an example of you running the command and showing real output here.

Redacted Command

 multi-gitter  run ./examples/general/replace.sh  --conflict-strategy replace --git-type cmd -m "test message" --token <faketoken> -g https://dev.azure.com/<my-org> -p azuredevops -R <ProjectName>/<RepoName> reviewers [email protected]

Redacted Output

INFO[0001] Running on 1 repositories                    
INFO[0001] Cloning and running script                    repo=<MyProject>/<MyRepo>
INFO[0003] Pushing changes to remote                     repo=<MyProject>/<MyRepo>
INFO[0004] Creating pull request                         repo=<MyProject>/<MyRepo>

What issue does it fix

Closes #475

If there is not an existing issue, please make sure we have context on why this change is needed. .

Notes for the reviewer

go is not my main language, but hopefully this is pretty close to what you would be looking for in a new VersionController for AzureDevops. I tried my best to follow existing implementations, but I'm happy to make any changes!

Checklist

  • [x ] Made sure the PR follows the CONTRIBUTING.md guidelines
  • [ x] Tests if something new is added

@lindell
Copy link
Owner

lindell commented Apr 23, 2024

Thanks for the PR! I will take a closer look in the coming days.

Just briefly browsing the cmd/ change I noticed the naming of things in Azure Devops seems to colide with the naming of GitLab. Just to make sure I understand the terminology correctly. Is this how things are named?

Github GitLab Azure Devops
Repository Project Repository
Organization Group (hierarchical) Project

So in Azure Devops the only levels are Repositories that can be within one project? There are no stacking projects, and no higher level (organizations etc)?

@rerikson
Copy link
Author

Hey thanks for the question! I tried to make the naming in the code match the naming in Azure DevOps and you're right it does collide with GitLab. The chart you made of Github/GitLab/Azure DevOps is correct. Azure DevOps does have the concept of organizations, but each organization would have a different base url and is pretty much a totally separate entity. There are no stacked/sub projects either.

Copy link
Owner

@lindell lindell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had time to look at some initial code in cmd. If you want to TAL, please do, otherwise feel free to wait until I have time to look at the rest of the code.

cmd/platform.go Outdated Show resolved Hide resolved
cmd/platform.go Outdated Show resolved Hide resolved
cmd/platform.go Outdated Show resolved Hide resolved
cmd/platform.go Outdated Show resolved Hide resolved
cmd/platform.go Show resolved Hide resolved
internal/scm/azuredevops/azuredevops.go Outdated Show resolved Hide resolved
@rerikson
Copy link
Author

rerikson commented May 2, 2024

Hey @lindell, no rush on the rest of the code review, but just wanted to let you know I pushed up a commit to address the comments so far.

Copy link
Owner

@lindell lindell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello again,

Nice that you had time to look at the first comment. I've had very little time to work on this the last week, but I've been able to go through about half of the PR. In general I think it's great since you are not usually working with Go.
I publish the new comments, but as the last time, please feel free to ignore them until the entire review is done if you want.

If there is one comment to take a look at, it's about getting repositories by name, and not fetching all of them.

internal/scm/azuredevops/azuredevops.go Outdated Show resolved Hide resolved
return nil, err
}

locationClient := location.NewClient(context.Background(), connection)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a context.Background here does mean that this request will not be cancelable (by pressing Ctrl+C or similar).

I think we should either create a new client for each time we need it, with a cache (cache seems to already be implemented internally in the library). But with a help method, func (a *AzureDevOps) connection(ctx context.Context) .

Or make sure we send in a context in the creation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand what you mean, but let me make sure. Are you suggesting I defer creating the clients until I actually need to use them inside the various methods: ForkRepository, GetRepositories, etc., pass the context into the NewClient method at that time, and store it on the *AzureDevOps reference?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should probably be the way to do it 🙂

projectMap := make(map[string]uuid.UUID)

if fork {
projects, err := coreClient.GetProjects(context.Background(), core.GetProjectsArgs{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this always return all projects? Even if there are thousands of them? If not I think it might be cleaner to get the name when needed (with the same cache structure), slightly more inefficient in some scenarios, and slightly less in others (with very new forks).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that will return all projects even if there are thousands. Are you thinking I should change this to a search by project name on demand?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that would be the way to do it if the API allows

internal/scm/azuredevops/azuredevops.go Show resolved Hide resolved
internal/scm/azuredevops/azuredevops.go Outdated Show resolved Hide resolved
}

func (a *AzureDevOps) getRepos(ctx context.Context) (*[]git.GitRepository, error) {
allRepos, err := a.gitClient.GetRepositories(ctx, git.GetRepositoriesArgs{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way of getting a repository by name? (project + name)

Will skip to review this function until I know.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked and there is a way of getting repositories by name as long as you also have the project name

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case (if we can avoid fetching all repositories in the entire org). We should instead do a fetching for only the projects / repositories defined.

internal/scm/azuredevops/azuredevops.go Show resolved Hide resolved
internal/scm/azuredevops/azuredevops.go Show resolved Hide resolved
return err
}

newObjectID := "0000000000000000000000000000000000000000"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment of what is happening here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment but let me know if it's not sufficient. I pulled this value from the docs here: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-7.1&tabs=HTTP

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the link to the docs as well and we should be good here

@lindell
Copy link
Owner

lindell commented May 9, 2024

Tried to review through vscode, apparently it posts every comment and not together. Sorry if this was spammy to you.

Copy link
Owner

@lindell lindell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now done with the review, thanks again for the PR!

Please let me know if you have any follow up questions, or if you need any assistance with fixing any of the comments. I can't guarantee any timeline, but I'm happy to help.

Comment on lines 491 to 494
if err == nil {
return a.convertRepo(existingFork), nil
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be corect the correct behaviour if the error is for example a network error. (A new fork will be requested).

Something similar to this need to be made:

Suggested change
if err == nil {
return a.convertRepo(existingFork), nil
}
if err == nil {
return a.convertRepo(existingFork), nil
} else if !errors.Is(err, core.NotFoundError) {
return nil, err
}

Note: I have not verified how to check notfound errors (core.NotFoundError does not exist) with this API. But it can either be a constant error (use errors.Is), or a type error (use errors.As)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a way to check not found errors that I think makes sense and pushed that up. Let me know if you think I should do something else please.

np = r.ownerName
}

repoName := r.name + "." + strings.ReplaceAll(currentUser, " ", ".")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forks can only be made to the current user in Azure devops? Not to projects (like to organizations in GitHub)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forks can actually only be made to Projects in Azure devops. In the lines above, I am appending the username to the end of the repo name so that multiple forks of the repo can be forked into the same project. The project to fork the repo into gets passed into this method through the newProject variable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this behavior be something users want to configure? I'm fine if you want to start with this, and potentially add it later.

internal/scm/azuredevops/azuredevops_test.go Show resolved Hide resolved
internal/scm/azuredevops/repository.go Outdated Show resolved Hide resolved
@rerikson
Copy link
Author

Thank you very much for the thorough PR and being kind to my go code! Also, sorry it took so long for me to get back to this. I'll try and get any remaining changes fixed much faster. Any changes I pushed up I closed the conversation for and I left open any conversations that seemed like further discussion might be needed.

I think we're getting close on this!

Copy link
Owner

@lindell lindell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long review time. Got it in the beginning of a longer vacation, and had forgot about it when I came back. Should be faster to respond to any subsequent messages.

return nil, err
}

locationClient := location.NewClient(context.Background(), connection)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should probably be the way to do it 🙂

projectMap := make(map[string]uuid.UUID)

if fork {
projects, err := coreClient.GetProjects(context.Background(), core.GetProjectsArgs{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that would be the way to do it if the API allows

}

func (a *AzureDevOps) getRepos(ctx context.Context) (*[]git.GitRepository, error) {
allRepos, err := a.gitClient.GetRepositories(ctx, git.GetRepositoriesArgs{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case (if we can avoid fetching all repositories in the entire org). We should instead do a fetching for only the projects / repositories defined.

internal/scm/azuredevops/azuredevops.go Show resolved Hide resolved
return err
}

newObjectID := "0000000000000000000000000000000000000000"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the link to the docs as well and we should be good here

np = r.ownerName
}

repoName := r.name + "." + strings.ReplaceAll(currentUser, " ", ".")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this behavior be something users want to configure? I'm fine if you want to start with this, and potentially add it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add Azure DevOps VersionController
3 participants